<?php
if ( !defined('BASEPATH') ) { exit( 'No direct script access allowed' ); }

class NurseryAdminAction {
    private $CI;
    private $db;
    private $rid;
    public $link;
    public $act_arr;
    public $menu_arr;
    public $menu_list;
    public $supper_menu_list;
    public $role_name;
    public $action= 'nursery_action';
    public $menu= 'nursery_menu';
    public $role= 'nursery_role';
    public $role_action = 'nursery_role_action';
    public $admin = 'nursery_admin';
    

    function __construct() {
    
        $this->CI = & get_instance();
        $this->CI->load->database();
        $this->db = $this->CI->db;
        
        //取当前登录管理员的角色id
        $this->rid = $this->CI->session->userdata('nrid');
        //取当前登录管理员的角色名称
        $this->role_name=$this->get_role_name();
        //获取超级管理员的左侧菜单
        $this->supper_menu_list = $this->get_all_menus();
        
        //获取非超级管理员的左侧菜单
        $this->menu_list = $this->get_current_role_left_menu();
       //  var_dump($this->menu_list);die;

    }

    /**
     * 判断是否为超级管理员
     * @return boolean
     */
    public function isSuperAdmin()
    {
        if (! $this->rid || $this->role_name == '超级管理员') {
            
            return true;
        }
        
        return false;
    }


    /**
     *  权限验证
     *  检查当前登录管理员是否拥有该链接的权限
     *  勿删除
     */
    public  function check_current_role_permissions()
    {  
        $role_status = false;
        $cur_role = $this->get_current_role_actions();

        if ( $this->link )
        {
        	$link = trim( $this->link,'/' );

        	$no_index_link = "";
        	if( strpos( $link, 'index' ) )
            {
        		$no_index_link = trim(substr($link,0, strpos( $link, 'index' ) ),'/');
        	}

            foreach ( $cur_role as $act )
            {
            		
                    if ( (trim($act['link'],'/') == $link)  || (trim($act['link'],'/') == $no_index_link)  )
                    {
                        $role_status = true;
                        break;
                    }
            }
        }
      
        if (!$role_status)
        {
            show_error('权限不够');
            exit;
        }

    }


    /**
     * 获取当前登录角色拥有的权限集合
     * （包括权限链接在内的所有字段的集合）
     * 返回的是二维数组
     * @return array
     */
    function get_current_role_actions()
    {
    	$myrole =array();
        $act_id_arr = $this->get_current_role_aid_arr();
        $all_act = $this->get_all_permissions();

        foreach ( $all_act as $key=>&$val )
        {
            if ( !empty($val) && is_array($val))
            {
                foreach ( $val as $k=>&$v )
                {
                    if ( in_array($v['id'], $act_id_arr) )
                    {
                    	$myrole[]= $val[$k];
                    }
                }
            }

        }

        return $myrole;
    }

    
    
 
    /**
     *  获取当前登录角色已经分配的权限id集合
     * 返回值 一维数组 元素值为权限id
     * @return array
     * @author wxd
     */
    function get_current_role_aid_arr()
    {
        $act_aid_arr = array();
        $this->db->select('aid');
        $this->db->from( $this->role_action );
        $this->db->where('rid', $this->rid);
        $query = $this->db->get();
        if ( $query->num_rows() > 0 )
        {
            $act = $query->result_array();
            foreach( $act as $e )
            {
                $act_aid_arr[] = $e['aid'];
            }

        }
        return  $act_aid_arr;

    }



    /**
     * 获取当前登录角色的左侧菜单
     * @return array
     */
    
    function get_current_role_left_menu()
    {
    	$link_array = array();
        //获取所有的菜单集合
    	$all_menus = $this->get_all_menus();
        //获取当前登录用户的所有权限集合
    	$my_action = $this->get_current_role_actions();
        //生成当前登录管理员所有能访问的链接
    	foreach( $my_action as $e )
        {
    		$link_array[] = $e['link'];
    	}

        //第一步 根据权限 获取用户当前能够拥有的左侧菜单

	     foreach ( $all_menus as $key=>&$val )
         {
             //当前顶级菜单的子菜单不为空且为数组的时候
             //否则注销该顶级菜单
             if (!empty($val) && is_array($val) )
             {
                 foreach ( $val as $k=>&$v )
                 {
                    if ( !in_array($v['link'], $link_array) )
                    {
                        unset($val[$k]);
                    }
                 }
             }else{
                    unset($all_menus[$key]);
             }

	     }

	     //第二步  在第一步的基础上 去除空的顶级导航
	     foreach ( $all_menus as $k=>&$e )
         {
	          if ( empty($e) ) { unset($all_menus[$k]); }
	     }

	   return  $all_menus;
    	
    }

    /**
     * 求解管理员的角色名称
     * @param $id
     * @return mixed
     * @author wxd
     */
    public  function  get_role_name()
    {
        $this->CI->load->model('nursery_role_model');
        $res = $this->CI->nursery_role_model->find(array('id' => $this->rid));
        return $res['name'];

    }

    /**
     * 获取菜单表中已经录入的所有的左侧菜单
     * 返回值二维数组 键值为顶级菜单的名称
     * @return array
     */
    public  function  get_all_menus()
    {
        $this->CI->load->model('nursery_menu_model');
        //获取顶级菜单集合
        $top_menus=$this->CI->nursery_menu_model->top_menus;
        $all_menus=array();
        foreach($top_menus as $key=> $val)
        {
            $all_menus[$val]=$this->CI->nursery_menu_model->get_child_menus($key);
        }
        return $all_menus;
    }



    /**
     * 获取权限表中已经录入的所有的权限集合
     * @return mixed
     */

    public  function  get_all_permissions()
    {

        $this->CI->load->model('nursery_menu_model');
        $top_permissions=$this->CI->nursery_menu_model->top_permissions;
        $all_permissions=array();
        foreach($top_permissions as  $val)
        {
            $all_permissions[$val]=$this->CI->nursery_menu_model->get_child_permissions($val);
        }
        return $all_permissions;

    }



}
